package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.*;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.pojo.Setmeal;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.*;

/**
 * 移动端预约服务
 */
/*
1、检查用户所选择的预约日期是否已经提前进行了预约设置，如果没有设置则无法进行预约
2、检查用户所选择的预约日期是否已经约满，如果已经约满则无法预约
3、检查用户是否重复预约（同一个用户在同一天预约了同一个套餐），如果是重复预约则无法完成再
次预约
4、检查当前用户是否为会员，如果是会员则直接完成预约，如果不是会员则自动完成注册并进行预约
5、预约成功，更新当日的已预约人数
 */
@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderSettingDao orderSettingDao;

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private OrderDao orderDao;
    @Autowired
    SetmealDao setmealDao;

    @Autowired
    private AddressDao addressDao;
    @Override
    public Result order(Map map) throws Exception {
        //1、检查用户所选择的预约日期是否已经提前进行了预约设置，如果没有设置则无法进行预约
        String orderDate = (String) map.get("orderDate");
        Date date = DateUtils.parseString2Date(orderDate);
        //查询是否在这个时间段设置了预约
        OrderSetting orderSetting = orderSettingDao.findOrderSetting(date);
        if (orderSetting == null) {
            //不可以预约
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        //2、检查用户所选择的预约日期是否已经约满，如果已经约满则无法预约
        int number = orderSetting.getNumber();
        int reservations = orderSetting.getReservations();
        if (number <= reservations) {
            //预约人数已经满了，不可以预约
            return new Result(false, MessageConstant.ORDER_FULL);
        }
        //3、检查用户是否重复预约（同一个用户在同一天预约了同一个套餐），如果是重复预约则无法完成再次预约
        String telephone = (String) map.get("telephone");
        //根据手机号查询会员信息
        Member member = memberDao.findByTelephone(telephone);
        if (member != null) {
            Integer member_id = member.getId();//会员id
            Date Orderdate1 = DateUtils.parseString2Date(orderDate);//日期
            String setmealId = (String) map.get("setmealId");//套餐
            Order order = new Order(member_id, Orderdate1, Integer.parseInt(setmealId));
            List<Order> list = orderDao.findByCondition(order);
            if (list != null && list.size() > 0) {
                //用户在重复预约
                return new Result(false, MessageConstant.HAS_ORDERED);
            }
        } else {
            //4、检查当前用户是否为会员，如果是会员则直接完成预约，如果不是会员则自动完成注册并进行预约
            member = new Member();
            member.setName((String) map.get("name"));
            member.setPhoneNumber(telephone);
            member.setIdCard((String) map.get("idCard"));
            member.setSex((String) map.get("sex"));
            //将会员信息进行添加注册
            memberDao.add(member);
        }
        //5、预约成功，更新当日的已预约人数
        //
        Order order = new Order(member.getId(), date, (String) map.get("orderType"),
                Order.ORDERSTATUS_NO, Integer.parseInt((String) map.get("setmealId")));
        //获取地址值
        String address = (String) map.get("address");
        Map map1= new HashMap<>();
        map1.put("position",address);
        //根据地址查询地址id
        Integer addressId=addressDao.findAddressById(map1);
        orderDao.add(order);
        Integer order_id1=order.getId();
        map1.put("addressId",addressId);
        map1.put("order_id",order_id1);
        //添加关系表
        addressDao.addAdressByorder(map1);
        //增加已预约人数
        orderSetting.setReservations(orderSetting.getReservations() + 1);
        orderSettingDao.setReservations(orderSetting);
        return new Result(true, MessageConstant.ORDER_SUCCESS, order.getId());
    }
    //根据id查询预约的详细信息
    @Override
    public Map findById(Integer id) {
        Map map = orderDao.findById4Detail(id);
        return map;
    }

    @Override
    public List<Integer> findDayOrderCount(List<String> dates) {
        List<Integer> orderCount= new ArrayList<>();
        if (dates !=null && dates.size()>0){
            for (String date : dates) {
                Integer orderCountByDate = orderDao.findOrderCountByDate(date);
                orderCount.add(orderCountByDate);
            }
        }
        return orderCount;
    }

    @Override
    public List<Integer> findDayVisitCount(List<String> dates) {
        List<Integer> visitCount= new ArrayList<>();
        if (dates !=null && dates.size()>0){
            for (String date : dates) {
                Integer visitCountByDate = orderDao.findVisitsCountByDate(date);
                visitCount.add(visitCountByDate);
            }
        }
        return visitCount;
    }

    @Override
    public List<Map<String,Object>> findNamesByDateAndMemberId(Map<String,Object> map) {

       List<Map<String,Object>> list= orderDao.findSetmealAndMemberName(map);
        return list;
    }

    @Override
    public Map<String, Object> findOrderDetail(Integer id) {
        Map<String,Object> map=orderDao.findOrderDetail(id);
        return map;
    }

    @Override
    public  List< Order> findOrderByMemberId(Integer id) {

        return orderDao.findOrderByMemberId(id);
    }

    @Override
    public String findAddressById(Integer id) {
        return orderDao.findAddressById(id);
    }

}
